package hc.bre.data.encoder

import hc.bre.cmn.IListOption
import hc.bre.cmn.ListOption
import hc.bre.cmn.RuleScriptBuilder

@groovy.transform.CompileStatic
class EncoderBuilder extends RuleScriptBuilder<EncoderDefinition>{
	@Override
	public void buildClassMemberClass() {
		String parent = className(ListOption.class)
		apdInnerClass("EncoderOption extends ${parent}"){
			apdNl("EncoderOption(Map map){")
					.indentInc()
					.apdNlBlock(IListOption.assignmentFromMap('map'))
					.indentDec()
					.apdNl("};");
		}
	}

	@Override
	public void buildClassMemberVariable() {
		ruleDef.list.each { EncoderItem item ->
			apdNl("public static final EncoderOption ${item.code} = ");
			buildVariableContent(item);
		}
	}

	void buildVariableContent(EncoderItem item) {
		apd("new EncoderOption([id:${item.id},code:${referText(item.code)},name:${referText(item.name)}])")
		if(!item.list) {
			apd(";");
		}else {
			apd("{")
			nested{
				item.list.each {
					apdNl("EncoderOption ${it.code} = ");
					buildVariableContent(it);
				};
			};
			apdNl("};")
		}
	}

	@Override
	public void buildClassRule() {
	}
}
